layui.config({
    base: "/infomssh/js/"
})
layui.use(['form', 'layer', 'laydate', 'table', 'upload', 'jquery_cookie'], function() {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        upload = layui.upload,
        table = layui.table;
    var cookie = layui.jquery_cookie();

    upload.render({ //允许上传的文件后缀
        elem: '#upload_btn',
        url: '/studentCash/uploadFile',
        headers: { 'Authorization': "Bearer " + cookie.cookie("token") },
        data: {
            grade: function() { return $('#gradeTime').val() },
            date: function() { return $('#yearMonthTime').val() },
            type: function() { return $('.fileType').val() }
        },
        accept: 'file', //普通文件
        exts: 'xls|xlsx', //只允许上传压缩文件
        before: function(obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.load(); //上传loading
        },
        done: function(res, index, upload) {
            layer.closeAll('loading'); //关闭loading

            var data;
            if (typeof(res) == "string") {
                var data = JSON.parse(res); //部分用户解析出来的是字符串，转换一下
            } else {
                data = res;
            }
            if (data.Code == 200) {
                top.layer.msg("上传成功");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            } else {
                layer.msg('上传失败。' + data.Message);
            }
        },
        error: function(index, upload) {
            layer.closeAll('loading'); //关闭loading
            layer.msg('上传失败。');
        }
    });

    upload.render({ //允许上传的文件后缀
        elem: '#uploadStu_btn',
        url: '/studentCash/uploadStudent',
        headers: { 'Authorization': "Bearer " + cookie.cookie("token") },
        accept: 'file', //普通文件
        exts: 'xls|xlsx', //只允许上传压缩文件
        before: function(obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.load(); //上传loading
        },
        done: function(res, index, upload) {
            layer.closeAll('loading'); //关闭loading

            var data;
            if (typeof(res) == "string") {
                var data = JSON.parse(res); //部分用户解析出来的是字符串，转换一下
            } else {
                data = res;
            }
            if (data.Code == 200) {
                top.layer.msg("上传成功");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            } else {
                layer.msg('上传失败。' + data.Message);
            }
        },
        error: function(index, upload) {
            layer.closeAll('loading'); //关闭loading
            layer.msg('上传失败。');
        }
    });

    $('#delete_btn').on('click', function() {
        layer.confirm('确定删除学生数据？', { icon: 3, title: '提示信息' }, function(index) {
            $.ajax({
                type: "Post",
                url: '/studentCash/deleteAllStudentInfo',
                headers: { 'Authorization': "Bearer " + cookie.cookie("token") },
                contentType: 'application/json;charset=utf-8',
                data: {}, //用ajax时必须序列化，不能直接用对象。
                success: function(strdata) {
                    var data;
                    if (typeof(strdata) == "string") {
                        var data = JSON.parse(strdata); //部分用户解析出来的是字符串，转换一下
                    } else {
                        data = strdata;
                    }
                    if (data.Code == 200) {
                        top.layer.msg("数据删除成功！");
                    } else {
                        layer.msg(data.Message);
                    }
                    layer.close(index);
                }
            });
        })
    })

    //年月范围选择
    laydate.render({
        elem: '#gradeTime',
        type: 'year',
        value: new Date()
    });
    laydate.render({
        elem: '#yearMonthTime',
        type: 'month',
        value: new Date()
    });



})